<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Akademik_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function addUniv($data) {


        $check = $this->db->prepare("select nama,kd_univ from universitas where nama=:nama and kd_univ=:kd_univ");

        $check->execute(array(
            ':kd_univ' => $data['kd_univ'],
            ':nama' => $data['nama']            
        ));

//        print_r($data);
        $result = $check->rowCount();
//        var_dump($result);

        if ($result > 0) {
//            echo 'data sudah ada';
        } else {
            $sth = $this->db->prepare
                    (
                    "INSERT INTO universitas 
                (kd_univ,nama)
                VALUES 
                (:kd_univ,:nama)"
            );
            $sth->execute(array(
                ':kd_univ' => $data['kd_univ'],
                ':nama' => $data['nama']
            ));
        }
    }

    public function univList() {

        $sth = $this->db->prepare("select univ_id,kd_univ,nama from universitas");

        $sth->execute();

        $data = $sth->fetchAll();

        return $data;

//        print_r($data);
    }

    public function deleteUniv($id) {

        $sth = $this->db->prepare("DELETE from universitas where univ_id = :id");

        $sth->execute(array(
            ':id' => $id
        ));
    }

    public function list_jurusan() {
        $sth = $this->db->prepare("SELECT * FROM jurusan");

        $sth->execute();

        $data = $sth->fetchAll();

        return $data;
    }

    public function add_jurusan() {

        $sth = $this->db->prepare("select nama from jurusan");

        $sth->execute();

        $data = $sth->fetchAll();

        return $data;
    }

    public function save_jurusan($data) {
        $array['univ'] = $data['0'];
        $array['kd_jurusan'] = $data['1'];

//        var_dump($array);
        $kd_jurusan = implode("_", $array);
//        var_dump($kd_jurusan);
//        var_dump($univ);
//        var_dump($nama);
        $sth = $this->db->prepare("INSERT INTO 
            jurusan 
            (kode_jurusan,nama,kd_universitas) 
            VALUES 
            (:kode_jurusan,:nama,:kd_universitas)
            ");
        $sth->execute(array(
            ':kode_jurusan' => $kd_jurusan,
            ':nama' => $data['2'],
            ':kd_universitas' => $data['0']
        ));
    }

    public function edit_jurusan($id) {
        $sth = $this->db->prepare("SELECT 
            * 
            FROM 
            jurusan
            WHERE
            id = :id");
        $sth->execute(array(
            ':id' => $id
        ));
        $data = $sth->fetch();
        return $data;
    }

    public function edit_save_jurusan($data) {
        $array['univ'] = $data['kd_univ'];
        $array['kd_jurusan'] = $data['kode_jurusan'];

//        var_dump($array);
        $kd_jurusan = implode("_", $array);

        $sth = $this->db->prepare("UPDATE jurusan 
            SET
            kode_jurusan = :kd_jurusan,
            nama= :nama,
            kd_universitas = :kd_universitas
            WHERE
            id = :id
            ");
        $sth->execute(array(
            ':kd_jurusan' => $kd_jurusan,
            ':nama' => $data['nama'],
            ':kd_universitas' => $data['kd_universitas'],
            ':id' => $data['id']
        ));
    }

    public function delete_jurusan($id) {
        $sth = $this->db->prepare("DELETE FROM 
            jurusan 
            WHERE
            id = :id");
        $sth->execute(array(
            ':id' => $id
        ));
    }

    public function list_makul() {
        $sth = $this->db->prepare("SELECT 
            *
            FROM
            makul");
        $sth->execute();
        $data = $sth->fetchAll();
        return $data;
    }

    public function save_makul($data) {
        $sth = $this->db->prepare("
            INSERT INTO
            makul
            (kd_makul,nama,kd_univ,kd_jurusan)
            VALUES
            (:kd_makul,:nama,:kd_univ,:kd_jurusan)
            ");
        $sth->execute(array(
            ':kd_makul'=>$data['kd_makul'],
            ':nama'=>$data['nama'],
            ':kd_univ'=>$data['kd_univ'],
            ':kd_jurusan'=>$data['kd_jurusan']
        ));
    }

    public function delete_makul() {
        
    }

}

?>
